/**
 * 添加或者修改页面
 */
var NewsInfoDlg = {
    data: {
        title: "",
        content: "",
        coverImagePath: "",
        delFlag: ""
    }
};

layui.use(['form', 'admin', 'ax','tinymce','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var tinymce = layui.tinymce;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: Feng.ctxPath +'/system/upload/'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
            $("#fileId").val(res.data.fileId);
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    var edit = tinymce.render({
        elem: "#content"
        , height: 600
        , width:'100%'
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/news/addItem", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });

        data.field.content = edit.getContent();

        ajax.set(data.field);

        ajax.start();

        return false;
    });

});